package model

type Config struct {
	Server     Server                `toml:"server"`
	Etcd       Etcd                  `toml:"etcd"`
	Datasource map[string]Datasource `toml:"datasource"`
	Nsq        Nsq                   `toml:"Nsq"`
	Influxdb   InfluxConfig          `toml:"Influxdb"`
}

type Server struct {
	Name string `toml:"name"`
	Port string `toml:"port"`
	Host string `toml:"host"`
}
type Etcd struct {
	URL      string `toml:"url"`
	Key      string `toml:"key"`
	Children []Etcd `toml:"children"`
}

type Datasource struct {
	DbType   string `toml:"dbType"`
	HostURL  string `toml:"url"`
	Database string `toml:"database"`
	Username string `toml:"username"`
	Password string `toml:"password"`
}
type Nsq struct {
	HostURL string `toml:"url"`
}
type InfluxConfig struct {
	HostURL string `toml:"url"`
	Bucket  string `toml:"bucket"`
	Org     string `toml:"org"`
	Token   string `toml:"token"`
}

type ConfigOpt func(config *Config)

func NewConfig(opt ...ConfigOpt) *Config {
	config := &Config{
		Server: Server{
			Name: "MainServer",
			Port: "8080",
			Host: "0.0.0.0",
		},
		Datasource: make(map[string]Datasource),
	}
	for _, c := range opt {
		c(config)
	}
	return config

}

func SetDatasource(key string, es Datasource) ConfigOpt {
	return func(h *Config) {
		h.Datasource[key] = es
	}
}
func SetEtcd(etcd Etcd) ConfigOpt {
	return func(h *Config) {
		h.Etcd = etcd
	}
}
func SetNsq(etcd Nsq) ConfigOpt {
	return func(h *Config) {
		h.Nsq = etcd
	}
}
func SetInfluxdb(influxdb InfluxConfig) ConfigOpt {
	return func(h *Config) {
		h.Influxdb = influxdb
	}
}
